﻿using Microsoft.AspNetCore.Mvc;
using WCS_Entity.Dto.AutoEquipmentDto;
using WCS_Entity.Dto;
using WCS_Service.Interface;
using WCS_Service.Services;
using WCS_Entity.Dto.MapPositionDto;

namespace WCS.Controllers
{
    /// <summary>
    /// AGV地图点位管理服务
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class MapPositionController : ControllerBase
    {
        private readonly IMapPositionService _mapPositionService;

        public MapPositionController(IMapPositionService mapPositionService)
        {
            _mapPositionService = mapPositionService;
        }

        /// <summary>
        /// 查询指定地图点位的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> QueryMapPositionAsync(int id)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _mapPositionService.QueryMapPositonAsync(id);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "地图点位信息获取失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 查询指定储位的地图点位
        /// </summary>
        [HttpPost]
        public async Task<WebApiCallBack> QueryMapPositionBaseBinNumberAsync([FromBody] QueryPositionDto dto)
        {
            var result = new WebApiCallBack();
            var dtoInfos = await _mapPositionService.QueryMapPositionBaseBinNumberAsync(dto.areaCode, dto.bindNumber);
            if (dtoInfos is null)
            {
                result.status = false;
                result.msg = "地图点位信息获取失败";
                return result;
            }
            result.data = dtoInfos;
            return result;
        }
        ///// <summary>
        ///// 查询指定储位和端口的地图点位
        ///// </summary>
        [HttpPost]
        public async Task<WebApiCallBack> QueryMapPositionBasePortNumberAsync([FromBody] QueryPositionPortDto dto)
        {
            var result = new WebApiCallBack();
            var dtoInfos = await _mapPositionService.QueryMapPositionBasePortNumberAsync(dto.areaCode,dto.bindNumber, dto.portNumber);
            if (dtoInfos is null)
            {
                result.status = false;
                result.msg = "地图点位信息获取失败";
                return result;
            }
            result.data = dtoInfos;
            return result;
        }
        /// <summary>
        /// 查询所有地图点位信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryAllMapPositionAsync()
        {
            var result = new WebApiCallBack();
            var dtoInfoList = await _mapPositionService.QueryAllMapPositonAsync();
            if (dtoInfoList is null)
            {
                result.status = false;
                result.msg = "所有地图点位信息获取失败";
                return result;
            }
            result.data = dtoInfoList;
            return result;
        }
        /// <summary>
        /// 删除指定的地图点位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<WebApiCallBack> DeleteMapPositionAsync(int id)
        {
            var result = new WebApiCallBack();
            result.status = await _mapPositionService.DeleteMapPositon(id);
            result.msg = result.status ? "删除成功" : "删除失败";

            return result;
        }
        /// <summary>
        /// 创建地图点位信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> CreatMapPositionAsync([FromBody] CreatMapPositionDto dto)
        {
            var result = new WebApiCallBack();
            if (string.IsNullOrEmpty(dto.mapPositionCode) ||
                string.IsNullOrEmpty(dto.mapCode) ||
                string.IsNullOrEmpty(dto.binNumber))
            {
                result.status = false;
                result.msg = "地图点位编号、地图编号、仓储号不能为空";
                return result;
            }
            var dtoInfo = await _mapPositionService.CreateMapPositonAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "创建地图点位信息失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 更新地图点位信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateMapPositionAsync([FromBody] UpdateMapPositionDto dto)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _mapPositionService.UpdateMapPositontAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "更新自动化设备信息";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
    }
}
